package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Consumption;
import cn.wolfcode.domain.ConsumptionItem;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.qo.SystemDictionaryItemQueryObject;
import cn.wolfcode.service.IConsumptionItemService;
import cn.wolfcode.service.IConsumptionService;
import cn.wolfcode.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("/consumptionItem")
@Controller
public class ConsumptionItemController {
    @Autowired
    private IConsumptionService consumptionService;
    @Autowired
    private IConsumptionItemService consumptionItemService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(ConsumptionItem consumptionItem) {
        //通过流水号查询出id
        Consumption consumption = consumptionService.selectByConsumptionCon(consumptionItem.getCno());
        if (consumptionItem.getId() == null) {
            consumptionItemService.save(consumptionItem);
        } else {
            consumptionItemService.update(consumptionItem);
        }
        return "redirect:/consumption/input?id="+consumption.getId();
    }

    @RequestMapping("/selectCategoryItem")
    @ResponseBody
    public List<SystemDictionaryItem> selectCategoryItem(SystemDictionaryItemQueryObject qo){
        if (!qo.getParentId().equals("''")){
            PageInfo<SystemDictionaryItem> query = systemDictionaryItemService.query(qo);
            return query.getList();
        }
        return null;
    }

    @RequestMapping("/delete")
    public String delete(Long[] ids,Long consumptionId){
        if (ids!=null){
            consumptionItemService.delete(ids);
        }
        return "redirect:/consumption/input?id="+consumptionId;
    }
}
